const service = require('../../../utils/service.js')
const api = require('../../../utils/util.js')
Page({
  data: {
    sendCode: true,
    bankList: '',
    codeTxt: '获取验证码',
    codeTime: 60,
    codeBol: true,
    bank_phone: ''
  },
  init: function() {
    let self = this,
      shop_code = wx.getStorageSync('shopInfo').shop_code,
      shopData = wx.getStorageSync('shop_data');
    api.ajax(service.url('getBank'), {
      unionid: shopData.shop_unionid,
      token: shopData.token,
      token_key: shopData.token_key,
      shop_code: shop_code
    }, function(res) {
      console.log("bank：" + JSON.stringify(res));
      if (res.code == 0) {
        self.setData({
          bankList: res.data,
          bank_phone: res.data.bank_phone || ''
        });
      } else {
        api.toast(res.message || "数据出现错误！");
      }
    });
  },
  sendCode: function() {
    let self = this,
      bank_phone = '';
    if (!this.data.codeBol) {
      return;
    }
    if (this.data.bank_phone == '' || this.data.bank_phone == null) {
      api.toast('请输入手机号码...');
      return;
    }
    if (!/^1[3456789][0-9]{9}$/.test(this.data.bank_phone)) {
      api.toast("手机号码输入有误！");
      return;
    }
    api.ajax(service.url('sendCode'), {
      phone: self.data.bank_phone,
      type: 5 //0:注册，1找回密码，2短信登录，3更改手机号，4修改手机号，5绑定银行卡
    }, function(res) {
      console.log("bind_code：" + JSON.stringify(res));
      if (res.code == 0) {
        self.setData({
          codeBol: false
        });
        let intervalId = setInterval(function() {
          self.setData({
            codeTxt: "秒后重新获取",
            codeTime: --self.data.codeTime
          });
          if (self.data.codeTime <= 0) {
            clearInterval(intervalId);
            self.setData({
              codeBol: true,
              codeTxt: '获取验证码',
              codeTime: 60
            })
          }
        }, 1000);
      } else {
        api.toast(res.message || '数据出现错误！');
      }
    });
  },
  //--获取手机号码
  getPhoneNumber: function(et) {
    let val = et.detail.value.replace(/\s+/g, "");
    if (!/^[0-9]+$/.test(val)) {
      val = this.data.bank_phone;
    }
    if (val.length == 11 && !/^1[3456789][0-9]{9}$/.test(val)) {
      api.toast("手机号码输入有误！");
    }
    this.setData({
      bank_phone: val
    });
  },
  addOrEditBank: function(et) {
    //{"bank_name":"","bank_number":"","bank_user":"","id_card":"","bank_phone":"","bank_code":""}
    let val = et.detail.value;
    val.bank_number = val.bank_number.replace(/\s+/g, "");
    val.id_card = val.id_card.replace(/\s+/g, "");
    if (val.bank_name == '' || val.bank_number == '' || val.bank_user == '' || val.id_card == '') {
      api.toast("请输入正确的银行卡信息！");
      return;
    }
    if (val.bank_user.length > 15) {
      api.toast("请输入正确的姓名！");
      return;
    }
    if (!/^[0-9]+$/.test(val.bank_number)) {
      api.toast("请输入正确的银行卡号！");
      return;
    }
    if (val.id_card.length > 18) {
      api.toast("身份证号码不能大于18位！");
      return;
    }
    if (!/^[0-9]{14,17}([0-9]{1}|X|x)$/.test(val.id_card)) {
      api.toast("请输入正确的身份证号码！");
      return;
    }
    if (val.bank_phone == '') {
      api.toast("请输入手机号码！");
      return;
    }
    if (!/^1[3456789][0-9]{9}$/.test(val.bank_phone)) {
      api.toast("手机号码输入有误！");
      return;
    }
    if (val.code == '') {
      api.toast("请输入验证码！");
      return;
    }
    val.bank_branch_name = '';
    val.shop_code = wx.getStorageSync('shopInfo').shop_code;
    let shopData = wx.getStorageSync('shop_data');
    val.unionid = shopData.shop_unionid;
    val.token = shopData.token;
    val.token_key = shopData.token_key;

    api.ajax(service.url('addOrEditBank'), val, function(res) {
      console.log("bind_bank" + JSON.stringify(res.data))
      if (res.code) {
        api.toast('绑定银行卡成功!');
        setTimeout(function() {
          wx.navigateBack();
        }, 500);
      } else {
        api.toast(res.message || '数据出现错误！');
      }
    });
  },
  /**
   * 生命周期回调—监听页面卸载
   */
  onUnload: function() {

  },
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.init();
  },
  /**
   * 页面显示/切入前台时触发
   */
  onShow: function() {

  }
})